package chapter04

import scala.util.control.Breaks

/**
 * Created with IntelliJ IDEA.
 * ClassName: Test06_Break
 * Package: chapter04
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 *          Date: 2023-06-19
 *          Time: 13:11
 */

object Test06_Break {
  def main(args: Array[String]): Unit = {
    //1.采用抛出异常的方式 实现 break的功能
    try {
      for (i <- 0 until 5){
        if (i == 3)
           throw new RuntimeException
          println(i)
      }
    } catch {
      case e: Exception =>  //模式匹配的形式 什么都不做 退出循环
    }
    println("这是循环代码块")
  }

  //import scala.util.control.Breaks._  相当于是.* 全部引入就可以省略Breaks
  //2.也是很麻烦 采用Scala中的Breaks类的break方法 实现异常抛出和捕获
  Breaks.breakable(
    for (i <- 0 until 5){
      if (i == 3)
        Breaks.break()
      println(i)
    }
  )


}
